import Vue from "vue";
import store from "@/store";
import VueRouter from "vue-router";
import Home from "../views/home/Home.vue";

Vue.use(VueRouter);
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};
const routes = [
  {
    path: "/",
    redirect: Home,
  },
  {
    path: "/home",
    name: "Home",
    component: Home,
  },
  {
    path: "/goods",
    name: "Goods",
    component: () => import("../views/allShop/Goods.vue"),
  },
  {
    path: "/user",
    name: "User",
    redirect: "/user/cart",
    component: () => import("../views/User.vue"),
    children: [
      {
        path: "cart",
        name: "Cart",
        component: () => import("../components/user/Cart.vue"),
      },
    ],
  },
  // {
  //   path: "/order",
  //   name: "Order",
  //   component: () => import("../views/Order.vue"),
  // },
  // {
  //   path: "/free",
  //   name: "Free",
  //   component: () => import("../views/Free.vue"),
  // },
  {
    path: "/details/:id",
    name: "Details",
    component: () => import("../views/detail/Details.vue"),
  },
  {
    path: "*",
    name: "Error",
    component: () =>
      import(/* webpackChunkName: "error" */ "../views/Error.vue"),
  },
];
const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
});
router.beforeEach((to, from, next) => {
  // console.log(to);
  let status = localStorage.getItem("x-auth-token");
  // console.log(to, from);
  if (
    status ||
    to.name == "Home" ||
    to.name == "Goods" ||
    to.name == "Details"
  ) {
    next();
    return;
  }
  store.dispatch("showToast/asyncChangeStatus", {
    content: "登录后查看",
    type: "info",
  });
  next({
    path: from.path,
  });
});
export default router;
